package com.bigevent.bigevent.controller;

import com.bigevent.bigevent.anno.State;
import com.bigevent.bigevent.entity.Article;
import com.bigevent.bigevent.entity.PageBean;
import com.bigevent.bigevent.entity.Result;
import com.bigevent.bigevent.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author yangxudong
 * @description: 文章控制层
 * @date 2024/2/10 11:25
 */
@RestController
@Validated
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articalService;

    @PostMapping
    public Result registArticle(@RequestBody @Validated Article article) {
        articalService.registArticle(article);
        return Result.success();
    }

    @PutMapping
    public Result updateArticle(@RequestBody @Validated(value = Article.update.class) Article article) {
        articalService.updateArticle(article);
        return Result.success();
    }

    @GetMapping
    public Result<PageBean<Article>> getArticleList(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                 @RequestParam(required = false, defaultValue = "10")Integer pageSize,
                                                 @RequestParam(required = false) Integer categoryId,
                                                 @RequestParam(required = false) @State String state) {
        return Result.success(articalService.getArticleList(pageNum, pageSize, categoryId, state));
    }

    @PostMapping("/upload")
    public Result<String> uploadArticleImage(@RequestParam("file") MultipartFile file) {
        return Result.success(articalService.uploadArticleImage(file));
    }

    @GetMapping("/detail")
    public Result<Article> getArticleDetail(@RequestParam Integer id) {
        return Result.success(articalService.getArticleDetail(id));
    }

    @DeleteMapping
    public Result deleteArticle(@RequestParam Integer id) {
        articalService.deleteArticle(id);
        return Result.success();
    }
}
